package org.graduates.webapp.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.projects.graduates.Constants;
import org.projects.graduates.app.GradApplication;
import org.projects.graduates.app.SecurityApplication;
import org.projects.graduates.domain.Person;
import org.projects.graduates.domain.PersonUser;
import org.projects.graduates.domain.User;
import org.projects.graduates.page.Pager;

import com.dayatang.domain.EntityRepository;
import com.dayatang.domain.InstanceFactory;
import com.dayatang.utils.WritableConfiguration;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("web")
public abstract class BaseAction extends ActionSupport {
	private static final long serialVersionUID = -1754469889670152640L;

	private static WritableConfiguration configuration;

	@Inject
	protected GradApplication gradApplication;

	@Inject
	protected SecurityApplication secApplication;

	@Inject
	private EntityRepository repository;

	/* 分页相关 */
	protected long total = 1;

	protected long pageSize = 10;

	protected long pageNumber = 1;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Pager pagerOf(Class clazz, String query) {
		String QL = "select count(o.id) from " + clazz.getName() + " o " + query;
		Map<String, Object> params = new HashMap<String, Object>();
		List<Long> counts = new ArrayList(getRepository().find(QL, params));
		long count = (null == counts ? 0 : counts.get(0).intValue());
		Pager result = new Pager(count, pageSize, pageNumber);
		// 设置总页数
		this.total = result.getTotalPage();
		return result;
	}

	private WritableConfiguration getConfiguration() {
		if (configuration == null) {
			configuration = InstanceFactory.getInstance(WritableConfiguration.class);
		}
		return configuration;
	}

	public void setConfiguration(WritableConfiguration configuration) {
		BaseAction.configuration = configuration;
	}

	public GradApplication getGradApplication() {
		return gradApplication;
	}

	public void setGradApplication(GradApplication gradApplication) {
		this.gradApplication = gradApplication;
	}

	public String getBasePath() {
		return ServletActionContext.getRequest().getContextPath();
	}

	public String getDateFormat() {
		return getConfiguration().getString(Constants.DATE_FORMAT);
	}

	public String getTimeFormat() {
		return getConfiguration().getString(Constants.TIME_FORMAT);
	}

	public String getDateTimeFormat() {
		return getConfiguration().getString(Constants.DATE_TIME_FORMAT);
	}

	public String getNumberFormat() {
		return getConfiguration().getString(Constants.NUMBER_FORMAT);
	}

	public String getPercentageFormat() {
		return getConfiguration().getString(Constants.PERCENTAGE_FORMAT);
	}

	public int getTableRows() {
		return getConfiguration().getInt(Constants.TABLE_ROWS, 30);
	}

	public String getCharset() {
		return getConfiguration().getString(Constants.CHARSET, "utf-8");
	}

	public String getUploadDir() {
		return getConfiguration().getString(Constants.UPLOAD_DIR, "/tmp");
	}

	public User getCurrentUser() {
		return (User) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
	}

	public Person getCurrentPerson() {
		return PersonUser.getPersonBy(getCurrentUser());
	}

	public long getCurrentPersonId() {
		return getCurrentPerson().getId();
	}

	public EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(EntityRepository.class);
		}
		return repository;
	}

	public void setRepository(EntityRepository repository) {
		this.repository = repository;
	}

	public SecurityApplication getSecApplication() {
		return secApplication;
	}

	public void setSecApplication(SecurityApplication secApplication) {
		this.secApplication = secApplication;
	}

	public String getContextRealPath() {
		return ServletActionContext.getServletContext().getRealPath(Constants.UPLOAD_DIR);
	}

	public String getWebAppName() {
		return ServletActionContext.getServletContext().getServletContextName();
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}

	public long getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(long pageNumber) {
		this.pageNumber = pageNumber;
	}

}
